package cc.chenwenxi.mypi.appium.kits;

import cc.chenwenxi.mypi.appium.entity.DriverInfo;
import cc.chenwenxi.mypi.appium.listener.AppiumAllListener;
import cc.chenwenxi.mypi.appium.listener.异常Listener;
import cc.chenwenxi.mypi.appium.listener.控件Listener;
import cc.chenwenxi.mypi.appium.listener.搜索Listener;
import cn.hutool.core.util.URLUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.events.EventFiringWebDriverFactory;
import io.appium.java_client.events.api.Listener;
import io.appium.java_client.remote.MobileCapabilityType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
public class DriverKit {
    private final DriverInfo driverInfo;

    private final static Map<String, AndroidDriver> drivers = Maps.newConcurrentMap();

    public AndroidDriver genDriver() {
        return Optional.ofNullable(drivers.get(driverInfo.getDriverName())).orElseGet(() -> {
            log.info("创建driver:{}", driverInfo);
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability(CapabilityType.BROWSER_NAME, "");//这句不是必须的
            capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, driverInfo.getDriverName());
            capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, driverInfo.getPlatformVersion());
            capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
//            capabilities.setCapability("appPackage", "com.haimai.baletu");
//            capabilities.setCapability("appActivity", "com.haimai.main.activity.SplashActivity");
            capabilities.setCapability("noReset", "True");
            // Android是否删除应用，IOS是否删除整个模拟器目录
            capabilities.setCapability("fullReset", false);
            // session覆盖
            capabilities.setCapability("sessionOverride", true);
            // 命令超时时间，默认60s
            capabilities.setCapability("newCommandTimeout", 18000);
            // 自动化引擎
//            capabilities.setCapability("automationName", "uiautomator2");
//            capabilities.setCapability("automationName","uiautomator1");
            // uiautomator2服务启动超时时间60s
//            capabilities.setCapability("uiautomator2ServerLaunchTimeout", 60000);
            // 屏蔽权限
            capabilities.setCapability("autoGrantPermissions", true);

            //盗用Appium自带的输入法
            capabilities.setCapability("unicodeKeyBoard", true);
            //default成设置前的配置
            capabilities.setCapability("resetKeyBoard", true);



            //解锁屏幕
//        capabilities.setCapability("unlockType","pattern");
//        capabilities.setCapability("unlockKey","12589");
//            AndroidDriver driver = new AndroidDriver(URLUtil.url("http://127.0.0.1:4723/wd/hub"), capabilities);
            AndroidDriver driver = new AndroidDriver(URLUtil.url("http://10.168.1.157:4723/wd/hub"), capabilities);


            List<Listener> listeners = Lists.newArrayList();
//            listeners.add(new 控件Listener());
//            listeners.add(new 异常Listener());
            listeners.add(new AppiumAllListener());
            driver = EventFiringWebDriverFactory.getEventFiringWebDriver(driver, listeners);

            driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); //second为等待时间，单位秒
            drivers.put(driverInfo.getDriverName(), driver);
            return driver;
        });
    }
}
